package web.admin.shop.product.tabs;

import domain.shop.Attribute;
import domain.shop.Product;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.value.ValueMap;
import service.facade.admin.ShopAdminService;
import web.commons.ConfirmModalWindow;

import java.util.logging.Level;
import java.util.logging.Logger;


public class ProductAttributesPanel extends Panel {
    private static Logger logger = Logger.getLogger(ProductAttributesPanel.class.getName());
    @SpringBean
    private ShopAdminService shopAdminService;
    private ListView attributesList;
    private Form mainForm;
    private ConfirmModalWindow confirmDeleteAttributeModalWindow;

    public ProductAttributesPanel(String id, final IModel<Product> productModel) {
        super(id, productModel);
        mainForm = new Form("form");
        add(mainForm);
        confirmDeleteAttributeModalWindow = new ConfirmModalWindow("cofirm", "Opravdu chcete vymazat atribut? Provedenou změnu nelze vrátit.") {

            @Override
            protected void onConfirm(AjaxRequestTarget target) {
                try {
                    shopAdminService.deleteProductAttribute(productModel.getObject(), ((IModel<Attribute>) getUserObjects()[0]).getObject());
                } catch (Exception e) {
                    logger.log(Level.SEVERE, null, e);
                    error(getLocalizer().getString("system.error", this));
                }
                target.addComponent(mainForm);
            }

            @Override
            protected void onCancel(AjaxRequestTarget target) {
            }
        };
        mainForm.addOrReplace(confirmDeleteAttributeModalWindow);

        attributesList = new ListView<Attribute>("attributesList", new PropertyModel(productModel, "attributes")) {

            @Override
            protected void populateItem(final ListItem<Attribute> item) {
                item.setOutputMarkupId(true);
                Form foo = new Form("attributesForm");
                foo.setOutputMarkupId(true);
                Integer index = item.getModelObject().getIndex();
                foo.setMarkupId("attributesForm" + index);

                AjaxButton button = new AjaxButton("delete") {

                    @Override
                    protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                        confirmDeleteAttributeModalWindow.show(target, item.getModel());
                    }
                };
                foo.add(button);
                button.setDefaultFormProcessing(false);
                item.add(foo);

                TextField field = new TextField("aName", new PropertyModel(item, "modelObject.name"));
                field.setOutputMarkupId(true);
                field.setMarkupId("aName" + index);
                foo.add(field);
                TextField field1 = new TextField("aValue", new PropertyModel(item, "modelObject.value"));
                field1.setOutputMarkupId(true);
                field1.setMarkupId("aValue" + index);
                foo.add(field1);
                TextField textField = new TextField("aUnit", new PropertyModel(item, "modelObject.unit"));
                textField.setOutputMarkupId(true);
                textField.setMarkupId("aUnit" + index);
                foo.add(textField);


            }
        };
        mainForm.add(attributesList);
        mainForm.add(new AddAttributeForm("addAttributeForm", productModel));
        setOutputMarkupId(true);
        attributesList.setOutputMarkupId(true);
        mainForm.add(new Button("save") {
            @Override
            public void onSubmit() {

                super.onSubmit();
                try {
                    shopAdminService.updateProduct(productModel.getObject());
                    info(getLocalizer().getString("saved", this));

                } catch (Exception e) {
                    logger.log(Level.SEVERE, null, e);
                    error(getLocalizer().getString("system.error", this));
                }
            }
        });
    }

    private class AddAttributeForm extends Form {

        public AddAttributeForm(String id, final IModel<Product> productModel) {
            super(id, new CompoundPropertyModel<ValueMap>(new ValueMap()));
            setOutputMarkupId(true);


            TextField field = new TextField("name");
            field.setOutputMarkupId(true);
            field.setType(String.class);
            add(field);
            field = new TextField("value");
            field.setOutputMarkupId(true);
            field.setType(String.class);
            add(field);
            field = new TextField("unit");
            field.setOutputMarkupId(true);
            field.setType(String.class);
            add(field);
            AjaxButton button = new AjaxButton("addAttribute") {


                @Override
                protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                    try {
                        ValueMap values = (ValueMap) AddAttributeForm.this.getModelObject();
                        Attribute attribute = new Attribute();
                        attribute.setName(values.getString("name"));
                        attribute.setUnit(values.getString("unit"));
                        attribute.setValue(values.getString("value"));
                        shopAdminService.addProductAttribute(productModel.getObject(), attribute);
                        values.clear();
                    } catch (Exception e) {
                        logger.log(Level.SEVERE, null, e);
                        error(getLocalizer().getString("system.error", this));
                    }
                    target.addComponent(mainForm);

                }
            };
            add(button);

        }
    }
}
